package com.kugou.miniapp.controller;

import com.kugou.miniapp.dao.MailUserMapper;
import com.kugou.miniapp.domain.Mail;
import com.kugou.miniapp.domain.Users;
import com.kugou.miniapp.service.MailService;
import com.kugou.miniapp.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class MailController {
    @Autowired
    MailService mailService;

    @Autowired(required = false)
    MailUserMapper mailUserMapper;

    @RequestMapping("send_mail")
    public int sendMail(Mail mail) throws Exception {
        return mailService.sendMail(mail);
    }

    @RequestMapping("get_mails")
    public List<Mail> getMails(String user_id){
        return mailService.getMailsByUserId(user_id.substring(0, 20));
    }

    @RequestMapping("del_mail")
    public ResponseData<String> delMail(@RequestParam(value = "mail_id",required = false) String mail_id, @RequestParam(value = "user_id",required = false)String user_id) throws Exception {
//        Integer count = mailService.delMailByMailId(mail_id);
        user_id = user_id.substring(0, 20);
        int count = 0;
        if (mail_id == null || mail_id.trim().length() == 0){
            throw new Exception("mail_id 不能为空");
        }
        if (user_id == null || user_id.trim().length()==0){
            throw new Exception("user_id 不能为空");
        }
        count = mailUserMapper.deleteMailUserByMailIdAndUserId(mail_id, user_id);
        if (count <= 0){
            throw new Exception("不存在该邮件或者删除失败");
        }
        if (count > 1){
            throw new Exception("删除过多记录，数据出现异常");
        }
        return ResponseData.genSuccess(new String("删除邮件成功"));
    }

//    @RequestMapping("/match_similar_list")
//    public List<Users> matchSimilarList(@RequestParam("userid") String userid){
//
//        return mailService.matchSimilarList(userid);
//    }

}
